package com.greate.community.event;

import com.alibaba.fastjson.JSONObject;
import com.greate.community.entity.Event;
import com.greate.community.service.DiscussPostService;
import com.greate.community.service.ElasticsearchService;
import com.greate.community.service.MessageService;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

@Component
@RocketMQMessageListener(topic = "${mq.system.Discussdel.totic}",consumerGroup ="${mq.Discussdel.consumer.group.name}" )
public class EventDiscussDelListener implements RocketMQListener<MessageExt> {
    private static final Logger logger = LoggerFactory.getLogger(EventDiscussDelListener.class);

    @Autowired
    private MessageService messageService;

    @Autowired
    private DiscussPostService discussPostService;

    @Autowired
    private ElasticsearchService elasticsearchService;
    @Override
    public void onMessage(MessageExt messageExt) {
        Event event=null;
        if (messageExt == null || messageExt.getBody() == null) {
            logger.error("消息的内容为空");
            return ;
        }
        try {
            // 解析内容
            String body = new String(messageExt.getBody(), "UTF-8");
            event = JSONObject.parseObject(body, Event.class);
            if (event == null) {
                logger.error("消息格式错误");
                return ;
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        elasticsearchService.deleteDiscusspost(event.getEntityId());
    }
}
